package top.huiger.newday.widget

import android.content.Context
import android.graphics.Color
import android.support.v4.content.ContextCompat.getColor
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.layout_bottom_menu.view.*
import top.huiger.newday.R

/**
 * 首页底部按钮
 *
 * Created by <huiGer> on 2018/1/8
 */
class BottomMenu : LinearLayout {

    var iSelResId: Int = -1
    var iSelColor: Int = -1
    var iUnSelResId: Int = -1
    var iUnSelColor: Int = -1
    var bChecked: Boolean = false

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {

        LayoutInflater.from(context).inflate(R.layout.layout_bottom_menu, this)

        val typedArray = context!!.obtainStyledAttributes(attrs, R.styleable.BottomMenu)
        iSelResId = typedArray.getResourceId(R.styleable.BottomMenu_bMenu_sel_img, R.mipmap.ic_launcher)
        iUnSelResId = typedArray.getResourceId(R.styleable.BottomMenu_bMenu_unSel_img, R.mipmap.ic_launcher)
        iSelColor = typedArray.getColor(R.styleable.BottomMenu_bMenu_sel_textColor, Color.BLACK)
        iUnSelColor = typedArray.getColor(R.styleable.BottomMenu_bMenu_unSel_textColor, getColor(context, R.color.gray))

        textView.text = typedArray.getString(R.styleable.BottomMenu_bMenu_text)
        imageView.setImageResource(iUnSelResId)
        typedArray.recycle()

    }

    fun onSelect() {
        imageView.setImageResource(iSelResId)
        textView.setTextColor(iSelColor)
        bChecked = true
    }

    fun unSelect() {
        imageView.setImageResource(iUnSelResId)
        textView.setTextColor(iUnSelColor)
        bChecked = false
    }

    fun isChecked(): Boolean {
        return bChecked
    }
}